// middleware/response_writer.go
package middleware

import (
	"bytes"

	"github.com/gin-gonic/gin"
)

// ResponseWriter 包装gin.ResponseWriter，用于捕获响应体
type ResponseWriter struct {
	gin.ResponseWriter
	body *bytes.Buffer // 存储响应体
}

// Write 重写Write方法，将响应数据写入buffer
func (w *ResponseWriter) Write(b []byte) (int, error) {
	w.body.Write(b)                  // 保存响应数据到buffer
	return w.ResponseWriter.Write(b) // 继续写入客户端
}

// WriteString 重写WriteString方法（可选，确保字符串响应也能被捕获）
func (w *ResponseWriter) WriteString(s string) (int, error) {
	w.body.WriteString(s)
	return w.ResponseWriter.WriteString(s)
}

// GetBody 获取捕获的响应体
func (w *ResponseWriter) GetBody() string {
	return w.body.String()
}
